package com.enen.service.impl;

import com.enen.mapper.ClazzMapper;
import com.enen.pojo.Clazz;
import com.enen.pojo.ClazzQueryParam;
import com.enen.pojo.Emp;
import com.enen.pojo.PageResult;
import com.enen.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    // 分页查询班级信息
    @Override
    public PageResult<Clazz> findAll(ClazzQueryParam clazzQueryParam) {
        //1.设置分页参数(PageHelper)
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        //2.执行查询
        List<Clazz> clazzList = clazzMapper.findAll(clazzQueryParam);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 遍历班级列表，设置班级状态
        for (Clazz clazz : clazzList) {
            LocalDate beginDate = clazz.getBeginDate();
            LocalDate endDate = clazz.getEndDate();
            if (currentDate.isAfter(endDate)) {
                clazz.setStatus("已结课");
            } else if (currentDate.isBefore(beginDate)) {
                clazz.setStatus("未开班");
            } else {
                clazz.setStatus("在读中");
            }
        }
        //3.解析查询结果，并封装
        Page<Clazz> p =(Page<Clazz>)clazzList;
        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }


    // 删除班级信息
    @Override
    public void delectById(Integer id) {
        clazzMapper.delectById(id);
    }
    // 添加班级信息
    @Override
    public void save(Clazz clazz) {
        // 将修改时间和更新时间置为当前时间
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);

    }
    // 根据id查询班级信息
    @Override
    public Clazz getInfo(Integer id) {
        return clazzMapper.getInfo(id);
    }
    // 查询所有用户信息
    @Override
    public List<Clazz> getAll() {
        return clazzMapper.getAll();
    }
    // 根据id修改班级信息
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }


}
